<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>可变变量</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.variables.scope.html">变量范围</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.variables.external.html">来自 PHP 之外的变量</a></div>
 <div class="up"><a href="language.variables.html">变量</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="language.variables.variable" class="sect1">
   <h2 class="title">可变变量</h2>

   <p class="simpara">
     有时候使用可变变量名是很方便的。就是说，一个变量的变量名可以动态的设置和使用。一个普通的变量通过声明来设置，例如：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="simpara">
    一个可变变量获取了一个普通变量的值作为这个可变变量的变量名。在上面的例子中
    <em class="emphasis">hello</em> 使用了两个美元符号（$）以后，就可以作为一个可变变量的变量了。例如：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">$</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'world'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="simpara">
    这时，两个变量都被定义了：<var class="varname"><var class="varname">$a</var></var> 的内容是“hello”并且
    <var class="varname"><var class="varname">$hello</var></var> 的内容是“world”。因此，以下语句：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">&nbsp;</span><span style="color: #007700">${</span><span style="color: #0000BB">$a</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="simpara">
    与以下语句输出完全相同的结果：
   </p>

   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #0000BB">$a</span><span style="color: #DD0000">&nbsp;</span><span style="color: #0000BB">$hello</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>

   <p class="simpara">
    它们都会输出：<span class="computeroutput">hello world</span>。
   </p>

   <p class="simpara">
    要将可变变量用于数组，必须解决一个模棱两可的问题。这就是当写下
    <var class="varname"><var class="varname">$$a[1]</var></var>
    时，解析器需要知道是想要
    <var class="varname"><var class="varname">$a[1]</var></var>
    作为一个变量呢，还是想要
    <var class="varname"><var class="varname">$$a</var></var>
    作为一个变量并取出该变量中索引为 [1]
    的值。解决此问题的语法是，对第一种情况用
    <var class="varname"><var class="varname">${$a[1]}</var></var>，对第二种情况用
    <var class="varname"><var class="varname">${$a}[1]</var></var>。
   </p>

   <p class="simpara">
    类的属性也可以通过可变属性名来访问。可变属性名将在该调用所处的范围内被解析。例如，对于
    <var class="varname"><var class="varname">$foo->$bar</var></var> 表达式，则会在本地范围来解析 <var class="varname"><var class="varname">$bar</var></var>
    并且其值将被用于 <var class="varname"><var class="varname">$foo</var></var> 的属性名。对于 <var class="varname"><var class="varname">$bar</var></var>
	是数组单元时也是一样。
   </p>
   <p class="simpara">
    也可使用花括号来给属性名清晰定界。最有用是在属性位于数组中，或者属性名包含有多个部分或者属性名包含有非法字符时（例如来自
    <span class="function"><a href="function.json-decode.html" class="function">json_decode()</a></span> 或 <a href="book.simplexml.html" class="link">SimpleXML</a>）。
   </p>

   <p class="para">
    <div class="example" id="example-106">
     <p><strong>Example #1 可变属性示例</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;bar.'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'I&nbsp;am&nbsp;A.'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;B.'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;C.'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$r&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'I&nbsp;am&nbsp;r.'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$foo&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$baz&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'foo'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'quux'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$bar&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$baz</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$start&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'b'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$end&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'ar'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">$start&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$end</span><span style="color: #007700">}&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'arr'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$foo</span><span style="color: #007700">-&gt;{</span><span style="color: #0000BB">$arr</span><span style="color: #007700">}[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

     <div class="example-contents"><p>以上例程会输出：</p></div>
     <div class="example-contents screen"><br />
I am bar.<br />
I am bar.<br />
I am bar.<br />
I am r.<br />
I am B.<br />
     </div>
    </div>
   </p>

   <div class="warning"><strong class="warning">Warning</strong>
    <p class="simpara">
     注意，在 PHP 的函数和类的方法中，<a href="language.variables.superglobals.html" class="link">超全局变量</a>不能用作可变变量。<em>$this</em>
	 变量也是一个特殊变量，不能被动态引用。
    </p>
   </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="language.variables.scope.html">变量范围</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="language.variables.external.html">来自 PHP 之外的变量</a></div>
 <div class="up"><a href="language.variables.html">变量</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
